package com.gxgeek.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

/**
 * @author gaoxing
 * @create 2018-04-02 下午6:54
 */
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {


//    @HystrixCommand(fallbackMethod = "fallback")
    //超时配置
//    @HystrixCommand(commandProperties ={
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "300")
//        }
//    )
    //熔断设置
    @HystrixCommand(commandProperties ={
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//设置熔断
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//请求达到阈值
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),
        }
    )
    @GetMapping("/getProductListByHystrix")
    public String getProductList(){
        RestTemplate restTemplate = new RestTemplate();

        String s = restTemplate.postForObject("http://127.0.0.1:8007/product/listForOrder",
                Arrays.asList("157875196366160022"),
                String.class);
        return s;
    }

    public String fallback(){
        return "用户过多,请稍后";
    }
    public String defaultFallback(){
        return "默认提示。用户过多,请稍后";
    }
}
